package com.huobao.yunying.activitys;

import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys_log_reg.Act_Login;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

public class Act_Hongbao extends HuobaoBaseActivity {

	@ViewInject(R.id.image)
	private ImageView image;
	@ViewInject(R.id.webview)
	private WebView webview;
	
	private boolean has_bonus;
	private String bonus_id;
	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_hongbao;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void init() {
		mAbTitleBar.setVisibility(View.GONE);
		getData();
		Drawable drawable = getResources().getDrawable(R.mipmap.xrhb1);
		LayoutParams layoutParams = (LayoutParams) image.getLayoutParams();
		layoutParams.width = ScreenUtil.getInstant().getDisWidth(context);
		layoutParams.height = ScreenUtil.getInstant().getDisWidth(context) * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
		image.setLayoutParams(layoutParams);
		image.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(SessionUtils.checkLogin(context)){
					if(has_bonus){
						Toast.makeText(context, "不能再领取了，贪心会长胖哦，再去掌婴转转吧！", Toast.LENGTH_SHORT).show();
					}else{
						receiverHongbao();
					}
				}else{
					Intent intent = new Intent(context, Act_Login.class);
					startActivity(intent);
				}
			}
		});
	}

	protected void receiverHongbao() {
		Map<String, String> map = HttpUtil.getParams("app","promotion","act","addbonus","redid",bonus_id);
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				Toast.makeText(context, jsonObject.getString("retval"), Toast.LENGTH_SHORT).show();
				finish();
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void getData(){
		Map<String, String> map = HttpUtil.getParams("app","promotion","act","bonus");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				String bonus_desc = jsonObject.getString("bonus_desc");
				bonus_id = jsonObject.getString("bonus_id");
				has_bonus = jsonObject.getBooleanValue("has_bonus");
				webview.loadDataWithBaseURL(null, bonus_desc, "text/html", "utf-8", null);
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}
